//
//  FaxianCell.m
//  网易新闻
//
//  Created by dllo on 15/10/10.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "FaxianCell.h"
#import "SDWebImage/UIButton+WebCache.h"
#import "Settings.h"
@interface FaxianCell()
@property(nonatomic, retain)UIButton *icon;
@property(nonatomic, retain)UILabel *name;
@property(nonatomic, retain)UIButton *image;
@property(nonatomic, retain)UILabel *title;
@property(nonatomic, retain)UILabel *subtitle;
@property(nonatomic, retain)UIButton *btn;
@property(nonatomic, retain)UILabel *subbtn;
@property(nonatomic, retain)FaxianModel *subMod;
@end
@implementation FaxianCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.name = [[UILabel alloc]init];
        self.name.font = [UIFont systemFontOfSize:15];
        self.name.textColor = [[Settings shareSettings]color:BlackToWhite];
        [self.contentView addSubview:self.name];
        [self.name release];
        
        self.title = [[UILabel alloc]init];
        self.title.textColor = [[Settings shareSettings]color:BlackToWhite];
        [self.contentView addSubview:self.title];
        [self.title release];
        
        self.subtitle = [[UILabel alloc]init];
        self.subtitle.font = [UIFont systemFontOfSize:15];
        self.subtitle.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.subtitle];
        [self.subtitle release];
        
        self.subbtn = [[UILabel alloc]init];
        self.subbtn.font = [UIFont systemFontOfSize:15];
        self.subbtn.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.subbtn];
        [self.subbtn release];
        
        self.image = [UIButton buttonWithType:UIButtonTypeCustom];
        self.image.backgroundColor = [UIColor lightGrayColor];
        [self.image addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.image];
        
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.layer.masksToBounds = YES;
        self.btn.layer.cornerRadius = 3;
        self.btn.layer.borderColor = [UIColor redColor].CGColor;
        self.btn.layer.borderWidth = 1;
        [self.btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [self.btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.btn];
        
        self.icon = [UIButton buttonWithType:UIButtonTypeCustom];
        self.icon.layer.masksToBounds = YES;
        self.icon.layer.cornerRadius = 3;
        [self.icon addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.icon];
        
        [self notificationHandle];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.icon.frame = CGRectMake(15, 20, 45, 45);
    self.name.frame = CGRectMake(70, 20, 290, 20);
    self.image.frame = CGRectMake(0, 45, 375, 215);
    self.title.frame = CGRectMake(15, 275, 345, 25);
    self.subtitle.frame = CGRectMake(15, 300, 345, 25);
    self.btn.frame = CGRectMake(15, 335, 105, 35);
    self.subbtn.frame = CGRectMake(135, 340, 200, 20);
}

- (void)setMod:(FaxianModel *)mod{
    if (_mod != mod) {
        [_mod release];
        _mod = [mod retain];
    }
    self.height = 385;
    [self.icon sd_setImageWithURL:self.mod.icon forState:UIControlStateNormal placeholderImage:nil];
    self.name.text = self.mod.name;
    self.subMod = [FaxianModel modWithDic:[[NSSet setWithArray:self.mod.context]anyObject]];
    [self.image sd_setImageWithURL:self.subMod.image forState:UIControlStateNormal placeholderImage:nil];
    self.title.text = self.subMod.title;
    self.subtitle.text = self.subMod.subtitle;
    [self.btn setTitle:self.subMod.btn forState:UIControlStateNormal];
    self.subbtn.text = self.subMod.subbtn;
}

- (void)click:(UIButton *)button{
    int type = (int)button.frame.origin.y;
    switch (type) {
        case 20:
            self.open(self.mod.jumpUrl);
            break;
        case 45:
            self.open(self.subMod.url);
            break;
        case 335:
            self.open(self.subMod.btnurl);
            break;
        default:
            break;
    }
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.title.textColor = [[Settings shareSettings]color:BlackToWhite];
        self.name.textColor = [[Settings shareSettings]color:BlackToWhite];
    }];
}

- (void)dealloc{
    [_name release];
    [_title release];
    [_subtitle release];
    [_subbtn release];
    [_subMod release];
    [super dealloc];
}
@end

